package com.oschina.wed.service.impl;

import com.oschina.wed.common.enums.VoucherFlowDirectionEnum;
import com.oschina.wed.common.utils.CommonUtils;
import com.oschina.wed.common.utils.SpringUtil;
import com.oschina.wed.dal.entity.AccountVoucherFlow;
import com.oschina.wed.dal.entity.AccountVoucherSumTmp;
import com.oschina.wed.service.AccountVoucherService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.List;

@JobHander(value="accountVoucherJob")
@Component("accountVoucherJob")
public class XxlJobExcuterServiceImpl extends IJobHandler {

    @Resource
    private AccountVoucherService accountVoucherService;

    @Override
    public ReturnT<String> execute(String... strings) throws Exception {
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();

        System.out.println("分片：" + shardingVO.getIndex() + "  计算启动....");
        accountVoucherService = (AccountVoucherService) SpringUtil.getBean("accountVoucherService");
        Long start = System.currentTimeMillis();
        boolean runFlag = true;
        while (runFlag) {
            Hashtable<String, Object> query = new Hashtable<>();
            query.put("dataIndex", shardingVO.getTotal());
            query.put("skipNum", 0);
            query.put("pageSize", 500);
            query.put("targetIndex", shardingVO.getIndex());
            List<AccountVoucherFlow> data = accountVoucherService.selectForStatics(query);
            if (data.size() < 500)
                runFlag = false;

            Hashtable<Long, AccountVoucherSumTmp> sum = new Hashtable<>();

            String voucherNos = "";

            for (AccountVoucherFlow item : data) {
                if (sum.containsKey(item.getAccountNo())) {
                    switch (VoucherFlowDirectionEnum.getByDirectionCode(item.getAccountDirection())) {
                        case IN:
                            sum.get(item.getAccountNo()).addAmount(item.getAmount());
                            break;
                        case OUT:
                            sum.get(item.getAccountNo()).subAmount(item.getAmount());
                            break;
                    }
                } else {
                    AccountVoucherSumTmp tmp = new AccountVoucherSumTmp();
                    tmp.setAccountNo(item.getAccountNo());
                    tmp.setAccountingDate(new SimpleDateFormat("yyyyMMdd").format(item.getPayDate()));
                    switch (VoucherFlowDirectionEnum.getByDirectionCode(item.getAccountDirection())) {
                        case IN:
                            tmp.setIncomeAmount(item.getAmount());
                            break;
                        case OUT:
                            tmp.setExpendAmount(item.getAmount());
                            break;
                    }
                    sum.put(item.getAccountNo(), tmp);
                }

                voucherNos = voucherNos.concat(item.getVoucherNo().toString()).concat(",");
            }

            for (AccountVoucherSumTmp accountVoucherSumTmp : sum.values()) {
                Hashtable<String, Object> sumVoucher = CommonUtils.convert2HashTable(accountVoucherSumTmp);

                if (accountVoucherService.updateAccountVoucherSum(sumVoucher) <= 0) {
                    accountVoucherService.insertAccountVoucherSum(sumVoucher);
                }
            }

            accountVoucherService.update2Staticed(voucherNos.substring(0, voucherNos.length() - 1));
        }
        Long end = System.currentTimeMillis() - start;
        System.out.println("分片：" + shardingVO.getIndex() + "  累计工作耗时：" + end + "ms");
        return ReturnT.SUCCESS;
    }
}
